package com.ztesoft.report.controller;

import com.ztesoft.report.base.controller.BaseController;
import com.ztesoft.report.base.pojo.R;
import com.ztesoft.report.constant.enums.DictType;
import com.ztesoft.report.entity.SysDict;
import com.ztesoft.report.entity.SysDictItem;
import com.ztesoft.report.entity.SysSql;
import com.ztesoft.report.service.SysDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
* 字典管理控制器
* @author 陈俊锋[296954345@qq.com]
* @date 2018/6/12
*/
@RestController
@RequestMapping("/sysDict")
public class SysDictController extends BaseController{
    @Autowired
    SysDictService sysDictService;

    private Logger logger = LoggerFactory.getLogger(SysDictController.class);

    @RequestMapping("/list")
    public Map list(@RequestBody Map<String, Object> map) {
        Map result = null;
        try{
            result = sysDictService.queryPage(map);
        } catch (Exception e) {
            logger.error("",e);
            result = R.error("查询失败,服务器异常，请联系相关人员！");
        }
        return result;
    }

    @RequestMapping("/sysDictList")
    public Map sysDictList(@RequestBody Map<String, Object> map) {
        Map result = null;
        try{
            result = sysDictService.sysDictList(map);
        } catch (Exception e) {
            logger.error("", e);
            result = R.error("查询失败,服务器异常，请联系相关人员！");
        }
        return result;
    }
    @RequestMapping("/addSysDict")
    public Map addSysDict(@RequestBody SysDictItem sysDictItem) {
        sysDictItem.setCreateBy(getCurrentUser());
        Map result = null;
        try{
            result = sysDictService.addSysDict(sysDictItem);
        } catch (RuntimeException e) {
            logger.error("新增失败,存在相同的字典选项编码无法新增！");
            result = R.error("新增失败,存在相同的字典选项编码无法新增！");
        }catch (Exception e) {
            logger.error("", e);
            result = R.error("新增失败,服务器异常，请联系相关人员！");
        }
        return result;
    }

    @RequestMapping("/editData")
    public Map editData(@RequestBody SysDict sysDict) {
        sysDict.setUpdateBy(getCurrentUser());
        Map map = null;
        try {
            map = sysDictService.editData(sysDict);
        } catch (RuntimeException e) {
            logger.error("编辑失败,存在相同的字典编码无法编辑修改！");
            map = R.error("编辑失败,存在相同的字典编码无法编辑修改！");
        } catch (Exception e) {
            logger.error("", e);
            map = R.error("编辑失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/editSysDict")
    public Map editSysDict(@RequestBody SysDictItem sysDictItem) {
        sysDictItem.setUpdateBy(getCurrentUser());
        Map map = null;
        try {
            map = sysDictService.editSysDictData(sysDictItem);
        } catch (RuntimeException e) {
            logger.error("编辑失败,存在相同的字典选项编码无法编辑修改！");
            map = R.error("编辑失败,存在相同的字典选项编码无法编辑修改！");
        }catch (Exception e) {
            logger.error("", e);
            map = R.error("编辑失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/removeData")
    public Map removeData(@RequestBody SysDict sysDict) {
        sysDict.setUpdateBy(getCurrentUser());
        Map map = null;
        try {
            map = sysDictService.removeData(sysDict);
        } catch (Exception e) {
            logger.error("", e);
            map = R.error("删除失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/addData")
    public Map addData(@RequestBody SysDict sysDict) {
        sysDict.setCreateBy(getCurrentUser());
        Map map = null;
        try {
            map = sysDictService.addData(sysDict);
        } catch (RuntimeException e) {
            logger.error("新增失败,存在相同的字典编码无法新增！");
            map = R.error("新增失败,存在相同的字典编码无法新增！");
        } catch (Exception e) {
            logger.error("", e);
            map = R.error("新增失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/removeSysDict")
    public Map removeSysDict(@RequestBody SysDictItem sysDictItem) {
        sysDictItem.setUpdateBy(getCurrentUser());
        Map map = null;
        try {
            map = sysDictService.removeSysDict(sysDictItem);
        } catch (Exception e) {
            logger.error("", e);
            map = R.error("删除失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/getSelectListByDictCode")
    public Map getSelectListByDictCode(@RequestBody SysDictItem sysDictItem) {
        Map map = null;
        try {
            map = sysDictService.getSelectListByDictCode(sysDictItem);
        } catch (Exception e) {
            logger.error("", e);
            map = R.error("删除失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    @RequestMapping("/getSelectListBySqlCode")
    public Map getSelectListBySqlCode(@RequestBody SysSql sysSql) {
        Map map = null;
        try {
            map = sysDictService.getSelectListBySqlCode(sysSql);
        } catch (Exception e) {
            logger.error("", e);
            map = R.error("获取失败,服务器异常，请联系相关人员！");
        }
        return map;
    }

    /**
     * 根据字典类型，字典code，字典值查询字典
     * @param map
     * @return
     */
    @RequestMapping("/getDictByCodeAndValue")
    public Map getDictByCodeAndValue(@RequestBody Map<String, Object> map){
        return sysDictService.getDictByCodeAndValue(getDictType(map.get("type")),(String) map.get("code"),(String) map.get("value"));
    }
    /**
     * 获取字典类型
     * @param obj
     * @return 1:普通字典 2:sql字典
     */
    private static DictType getDictType(Object obj){
        return obj != null && "2".equals(String.valueOf(obj)) ? DictType.SQL : DictType.DICT;
    }
}
